package cn.joyconn.cms.views.directive.sys;

// Generated 2016-1-20 11:19:19 by com.publiccms.common.source.SourceGenerator

import cn.joyconn.cms.common.base.AbstractController;
import cn.joyconn.cms.common.base.AbstractTemplateDirective;
import cn.joyconn.cms.controller.api.ApiController;
import cn.joyconn.cms.entities.sys.SysApp;
import cn.joyconn.cms.entities.sys.SysDomain;
import cn.joyconn.cms.entities.sys.SysSite;
import cn.joyconn.cms.entities.sys.SysUser;
import cn.joyconn.cms.logic.service.sys.SysDomainService;
import cn.joyconn.cms.logic.service.sys.SysSiteService;
import cn.joyconn.cms.logic.service.sys.SysUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.joyconn.cms.common.base.Base;
import com.joyconn.cms.common.directive.BaseTemplateDirective;
import com.joyconn.cms.common.directive.HttpDirective;
import com.joyconn.cms.common.handler.HttpParameterHandler;
import com.joyconn.cms.common.handler.PageHandler;
import com.joyconn.cms.common.handler.RenderHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import static cn.joyconn.cms.common.base.AbstractFreemarkerView.CONTEXT_SITE;
import static com.joyconn.cms.common.tools.CommonUtils.empty;
import static com.joyconn.cms.common.tools.CommonUtils.notEmpty;
import static org.apache.commons.lang3.ArrayUtils.contains;
import static org.apache.commons.lang3.StringUtils.split;

/**
 *
 * SysSiteListDirective
 * 
 */
@Component
public class SysSiteListByUserDirective extends AbstractTemplateDirective {
    @Autowired
    private SysSiteService service;
    @Autowired
    private SysDomainService domainService;
    @Autowired
    private SysUserService userService;


    @Override
    public void execute(RenderHandler handler) throws  Exception {
        Long uid = handler.getLong("uid");
        SysUser user = null;
        if (notEmpty(uid)) {
            user= userService.getEntity(uid);
        }
        handler.put("sysSites", Lists.newArrayList());
        if(user!=null){
            PageHandler pageHandler = userService.findByName(user.getName());
            if(pageHandler!=null&&pageHandler.getTotalCount()>0){
                List<SysUser> users = (List<SysUser>)pageHandler.getList();
                if(users!=null){
                    HashSet<Integer> siteIdSet = new HashSet<>();
                    for(SysUser sysUser:users){
                        siteIdSet.add(sysUser.getSiteId());
                    }
                    Integer[] siteIds= new Integer[siteIdSet.size()];
                    siteIdSet.toArray(siteIds);
                    List<SysSite> sysSites = service.getEntitys(siteIds);
                    if(sysSites!=null){
                        handler.put("sysSites", sysSites);
                    }
                }
            }
        }

        handler.render();


    }

}